(function(exports,virtualDom){
    var h = virtualDom.h    
    function createRender(el){
      var root = null
      var currentDom = null
      var rootfn = null
      return function render(fn){
        var vdom = null
        if(fn == null){
          vdom = rootfn()
        }else{
          rootfn = fn
          vdom = fn()
        }
        if(isVirtualNode(vdom)){
          void null
        }else{
          vdom = arrayToVirtualNode(vdom)
        }
        if(root == null){
          currentDom = vdom
          root = virtualDom.create(vdom)
          document.body.appendChild(root)
          return vdom
        }else{
          var patch = virtualDom.diff(currentDom,vdom)
          virtualDom.patch(root,patch)
          currentDom = vdom
          return vdom
        }
      }
    }
    function isFunction(a){
      return typeof a === 'function'
    }
    function isArray(a){
      return Object.prototype.toString.call(a) == '[object Array]'
    }
    function isVirtualNode(a){
      return a&&a.type == 'VirtualNode'
    }
    function isObject(a){
      return Object.prototype.toString.call(a) == '[object Object]'
    }
    function isString(a){
      return Object.prototype.toString.call(a) =='[object String]'
    }
    function arrayToVirtualNode(xs){
      if(xs.length == 0){
        throw []
      }else if(isString(xs)){
        return xs
      }else{
        var tag = xs[0]
        var attr = {}
        var tail = []
        if(isObject(xs[1])){
          attr = xs[1]
          tail = xs.slice(2)
        }else{
          tail = xs.slice(1)
        }
        return h(tag,attr,childToNode(tail))
      }
      function childToNode(child){
        return child.map(function(b){
          if(isArray(b)){
            return arrayToVirtualNode(b)
          }else if(isFunction(b)){
            return arrayToVirtualNode(b())
          }else{
            return b
          }
        })
      }
    }
    function eventHelper(){
      function eachPath(fn,e){
        if(e!=null&&isArray(e.path)&&false){
          e.path.forEach(fn)
        }else{
          if(e == null){
            return
          }else{
            var el = e.target || e
            fn(el)
            eachPath(fn,el.parentNode)
          }
        }
        
        function isSupportPath(obj){
         return obj&&obj.path
        }

      }
      // 点击事件
      document.body.addEventListener('click',function(e){
        eachPath(handler,e)
        function handler(el){
          if(isFunction(el._click)){
            el._click()
          }else{
            void null
          }
        }
      })
    }
    exports.mytemplate = {
      createRender:createRender,
      eventHelper:eventHelper
    }
}(this,virtualDom))